import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:lcapp/api/interface/comcon_api.dart';
import 'package:lcapp/api/request/response_data.dart';
import 'package:lcapp/config/const_config.dart';
import 'package:lcapp/data/app_update_data_entity.dart';
import 'package:lcapp/model/dialog/confirm.dart';
import 'package:lcapp/model/param/base_param.dart';
import 'package:lcapp/page/update/app_update.dart';
import 'package:lcapp/routes/route.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:url_launcher/url_launcher_string.dart';

class AppUpgrade {

  static AppUpdateDataEntity? entity;
  static bool isUpdate = false;

  //判断是否升级
  static Future<void> check({bool fromNet = false}) async {
    BaseParam.getParams();
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    String appId = AppId;
    String version = packageInfo.version;
    String? platform =
    Platform.isAndroid ? "android" : Platform.isIOS ? "ios" : '';
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();

    Map<String, dynamic> map = {
      'appId': appId,
      'version': version,
      'platform': platform,
    };
    ResponseData res = await CommonAPI.getAppUpgrade(map);
    sharedPreferences.setBool("isForce", false);
    if (res.code == 200 && res.data != null) {
      entity = AppUpdateDataEntity().fromJson(res.data);
      isUpdate = false;

      if(Platform.isIOS&&entity?.iosUpgrade==1&&entity?.iosUrl!=null){
        isUpdate = entity?.iosUpgrade==1;
      }else if(Platform.isAndroid&&entity?.androidUpgrade==1&&entity?.androidUrl!=null){
        isUpdate = entity?.androidUpgrade==1;
      }
      if(isUpdate){
        SmartDialog.show(
            alignment: Alignment.center,
            clickMaskDismiss: false,
            maskColor: Color.fromRGBO(0, 0, 0, 0.75),
            builder: (BuildContext context) {
              return AppUpdatePage(entity: entity!,);
            }
        );
      }
    }
  }

}

